﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using tuwien.auto.eibclient;

namespace KNXFramework.KNXObjects.ValueObjects
{
    internal class U16ObjectValue : UIntObjectValue
    {
        public U16ObjectValue(string value) : base(value)
        {
            if (m_Value > 65535)
                throw new ApplicationException("U16ObjectValue: Value must be contained between 0 and 65535. Val : " + value);
        }
        public U16ObjectValue(EIBBuffer buffer)
        {
            uint newValue;
            if (buffer.data.Length == 4)
            {
                newValue = (uint)((buffer.data[2] << 8) | buffer.data[3]);
                m_Value = newValue;
            }
            else
                throw new ApplicationException("U16ObjectValue: wrong packet received (too short)");
        }
        public override void AdaptBufferBeforeSending(bool isWrite, EIBBuffer buffer)
        {
            byte[] b = new byte[4];
            b[0] = buffer.data[0];
            b[1] = (byte)(isWrite ? 0x80 : 0x40);
            b[2] = (byte)((m_Value & 0xff00)>>8);
            b[3] = (byte)(m_Value & 0xff);
            buffer.data = b;
        } 
    }
}
